package com.example.shopping.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.example.shopping.R;
import com.example.shopping.bean.Response;
import com.example.shopping.utils.Constants;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.HashMap;

import okhttp3.Call;

public class ModifyActivity extends AppCompatActivity {

    private EditText ed_password;
    private EditText ed_toPassword;
    private TextView textView;

    private Button btn_modify;

    private String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        ed_password = findViewById(R.id.ed_password);
        ed_toPassword = findViewById(R.id.ed_toPassword);
        btn_modify = findViewById(R.id.btn_modify);

        textView = findViewById(R.id.warning);
        // 判断两次密码是否相同
        ed_toPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                //失去焦点处理
                if(!hasFocus){
                    String password = ed_password.getText().toString().trim();
                    String toPassword = ed_toPassword.getText().toString().trim();

                    //判断两个密码是否相等
                    if(password.equals(toPassword)){
                        // 如果密码相同，警告文字不会出现
                        textView.setVisibility(View.INVISIBLE);
                    }else{
                        // 如果密码不等，警告文字出现
                        textView.setVisibility(View.VISIBLE);
                    }

                }
            }
        });


        // 点击提交事件
        Intent intent = getIntent();
        String email = intent.getSerializableExtra("email").toString();

        btn_modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 两次密码相等
                if(textView.getVisibility() == View.INVISIBLE){

                    HashMap<String, String> map = new HashMap<>();
                    map.put("email",email);
                    map.put("password",ed_password.getText().toString().trim());
                    OkHttpUtils
                            .get()
                            .params(map)
                            .url(Constants.Login_UpdatePassword)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Call call, Exception e, int id) {
                                    Log.e("BtnOnError",e.getMessage());
                                }

                                @Override
                                public void onResponse(String response, int id) {
                                    Log.e("BtnOnResponse",response);
                                    result = response;
                                    result();
                                }
                            });
                }
            }
        });

    }

    public void result(){
        if(result != null){

            Response response = JSON.parseObject(result, Response.class);

            Toast.makeText(this,response.getMessage(),Toast.LENGTH_SHORT).show();
            // 请求成功
            if (response.getCode() == 200000){
                startActivity(new Intent(this,LoginActivity.class));
            }

        }
    }



}